
var payUtil = require('../../utils/uct_pay.js');

// pages/test/test.js
Page({

  /**
   * 页面的初始数据
   */

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options){
    // let app = getApp();
    // new app.Toast();
  },

  onGetCode: function(){
    wx.login({
      success: function (res) {
        console.log('u-code:' + res.code);
        wx.getUserInfo({
          success: function (userinfo) {
            console.log('u-enData:' + userinfo.encryptedData);
            console.log('u-iv:' + userinfo.iv);
            // console.log('userinfo:'+JSON.stringify(userinfo));
          }
        });
      }
    });
  },

  orderNoInput: function (e) {
    this.data.orderNo = e.detail.value
  },
  moneyInput: function (e) {
    this.data.money = e.detail.value
  },

  onPay: function () {
    payUtil.generateOrder(this.data.orderNo);
  },

  onRecharge: function () {
    util.recharge(this.data.money);
  },

  onLogin: function () {
    wx.login({
      success: function (res) {
        wx.getUserInfo({
          success: function (user) {
            console.log('u-code:' + res.code);
            console.log('u-enData:' + user.encryptedData);
            console.log('u-iv:' + user.iv);
            // console.log('userinfo:'+JSON.stringify(userinfo));
            wx.request({
              // url: 'http://www.qinghe.cc/api/user/register',
              url: 'http://www.liangyan.cc/api/user/register',
              data: {
                "code": res.code,
                "mobile": '13590394462',
                "password": '123321',
                "nickname": user.userInfo.nickName,
                "user_img": user.userInfo.avatarUrl,
              },
              method: 'POST',
              success: function (data) {
                data = data.data;
                console.log(data);
              }
            });
          }
        });
      }
    });
  },

  loadInfo: function () {
    wx.login({
      success: function (res) {
        wx.getUserInfo({
          success: function (user) {
            console.log('u-code:' + res.code);
            console.log('u-enData:' + user.encryptedData);
            console.log('u-iv:' + user.iv);
            // console.log('userinfo:'+JSON.stringify(userinfo));
            wx.request({
              // url: 'http://www.liangyan.cc/api/user/decrypt?XDEBUG_SESSION_START=PHPSTORM',
              url: 'http://120.79.62.239/liangyan/public/index.php/api/user/decrypt?XDEBUG_SESSION_START=PHPSTORM',
              header: {
                "appletId":"47cbcc3aef82744ba6ec00737ed1c8a7"
              },
              data: {
                "code": res.code,
                "signature": user.signature,
                "encryptedData": user.encryptedData,
                "iv": user.iv,
                "rawData": user.rawData,
              },
              method: 'POST',
              success: function (res) {
                wx.setStorageSync("USER_INFO", res.data.data);
                wx.setStorageSync("USER_TOKEN", res.data.data.token);
                console.log(res.data.data);
              }
            });
          }
        });
      }
    });
    
  },


  autoLogin: function () {
    wx.login({
      success: function (res) {
        wx.request({
          // url: 'http://www.uctshop.cc/api/user/login_code?XDEBUG_SESSION_START=PHPSTORM',
          url: 'http://www.liangyan.cc/api/user/login_code?XDEBUG_SESSION_START=PHPSTORM',
          header: {
            "appletId": "47cbcc3aef82744ba6ec00737ed1c8a7"
          },
          data: {
            "code": res.code,
          },
          method: 'POST',
          success: function (res) {
            wx.setStorageSync("USER_INFO", res.data.data);
            wx.setStorageSync("USER_TOKEN", res.data.data.token);
            console.log(res.data.data);
          }
        });
      }
    });
  },

  checkToken: function () {
    wx.login({
      success: function (res) {
        wx.request({
          url: 'http://www.uctshop.cc/api/user/check?XDEBUG_SESSION_START=PHPSTORM',
          header: {
            "appletId": "47cbcc3aef82744ba6ec00737ed1c8a7"
          },
          data: {
            "token": wx.getStorageSync("USER_TOKEN"),
          },
          method: 'POST',
          success: function (res) {
            console.log(res.data);
          }
        });
      }
    });
  },

  onWxLogin: function () {
    wx.login({
      success: function (res) {
        wx.getUserInfo({
          success: function (user) {
            console.log('u-code:' + res.code);
            console.log('u-enData:' + user.encryptedData);
            console.log('u-iv:' + user.iv);
            // console.log('userinfo:'+JSON.stringify(userinfo));
            wx.request({
              url: 'http://chinwoeco.com/api/user/info?XDEBUG_SESSION_START=PHPSTORM',
              data: {
                "code": res.code,
                "signature": user.signature,
                "encryptedData": user.encryptedData,
                "iv": user.iv,
                "rawData": user.rawData,

                //  "mobile": '13590394462',
                //  "password": '123321',
                //  "nickname": user.userInfo.nickName,
                //  "user_img": user.userInfo.avatarUrl,
              },
              method: 'POST',
              success: function (data) {
                data = data.data;
                console.log(data);
              }
            });
          }
        });
      }
    });
  },

  showToast: function () {
    var _this = this;
    // toast时间 
    _this.data.count = parseInt(_this.data.count) ? parseInt(_this.data.count) : 3000;
    // 显示toast 
    _this.setData({
      isShowToast: true,
    });
    // 定时器关闭 
    setTimeout(function () {
      _this.setData({
        isShowToast: false
      });
    }, _this.data.count);
  },


  /* 点击按钮 */
  clickBtn: function () {
    console.log("你点击了按钮");
    //设置toast时间，toast内容 
    this.show("你点击了按钮sss!");
  }

})